<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自习室审核</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css">
</head>
<body>
<table id="demo" lay-filter="test"></table>
<script src="https://www.layuicdn.com/layui/layui.js"></script>
<script>
    layui.use(['table', 'layer', 'jquery'], function () {
        var table = layui.table;
        var layer = layui.layer;
        let $ = layui.jquery;

        table.render({
            elem: '#demo',
            height: 'auto',
            url: '/studyRoom/getApplyResults', //数据接口,
            page: true, //开启分页
            cols: [[ //表头
                {field: 'roomApplyId', title: '申请编号', width: "20%",},
                {
                    field: 'studyroomId', title: '自习室编号', width: "20%", templet(data) {
                        return data.studyRooms.studyroomId;
                    }
                },
                {
                    field: 'studyroomNumber', title: '自习室楼号', width: "20%", templet(data) {
                        return data.studyRooms.studyroomNumber;
                    }
                },
                {
                    field: 'status', title: '审核状态', width: "20%", templet: function (data) {
                        return "需要处理的申请";
                    }
                },
                {
                    title: '操作', width: "20%", templet(data) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="agree">通过</a>\n' +
                            '    <a class="layui-btn layui-btn-xs" lay-event="refuse">不通过</a>';
                    }
                },
            ]],
            response: {
                statusCode: 200,//规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
        });

        table.on('tool(test)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            layer.confirm('是否要执行此操作', function(index){
                if (layEvent === 'agree') {
                    // 同意
                    apply(data.roomApplyId,'1',obj);
                } else if (layEvent === 'refuse') {
                    // 不通过
                    apply(data.roomApplyId,'2',obj);
                }
                layer.close(index);
            });


        })

        function apply(id, status,obj) {
            $.post('/studyRoom/handleApply', {'roomApplys.roomApplyId': id, 'approveOrnot': status}, response => {
                if (response.code==='200'){
                    layer.msg('操作成功', {icon: 1,time: 2000});
                    obj.del();
                }else {
                    layer.msg(response.msg, {icon: 2});
                }
            })
        }

    })
</script>
</body>
</html>